package com.company.cloud.work.modules.sdk.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.sdk.core.config.WxApiConfig;
import com.company.cloud.sdk.core.constant.SdkReturnEnum;
import com.company.cloud.sdk.core.enums.WxApiUrlEnum;
import com.company.cloud.sdk.core.utils.ApiUtil;
import com.company.cloud.work.modules.sdk.models.WxUserModel;
import com.company.cloud.work.modules.sdk.models.wxmodels.WxUserInfo;
import com.company.cloud.work.modules.sdk.models.wxmodels.WxUserListResult;
import com.company.cloud.work.modules.sdk.request.GetUserListReq;
import com.company.cloud.work.modules.sdk.request.GetUserReq;
import com.company.cloud.work.modules.sdk.response.WxUserInfoResponse;
import com.company.cloud.work.modules.sdk.response.WxUserListResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class WorkWxApi {

    /*企业微信地址*/
    private static final String QYWX_URL = "https://qyapi.weixin.qq.com/cgi-bin/";

    /**
     * 发送get请求
     * @param enums
     * @param map
     * @return
     */
    public static String doGet(WxApiUrlEnum enums, Map<String,String> map){
        String realUrl = WorkWxApi.QYWX_URL.concat(enums.getUrlType()).concat("?").concat(ApiUtil.joinParams(map));
        return HttpUtil.get(realUrl, CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * 发起post请求
     * @param enums
     * @param map
     * @return
     */
    public static String doPost(WxApiUrlEnum enums, Object map){
        String realUrl = WorkWxApi.QYWX_URL.concat(enums.getUrlType());
        return HttpUtil.post(realUrl, JSONObject.toJSONString(map));
    }

    /**
     * 获取access_token
     * @param wxConfig
     * @return
     */
    public static String getToken(WxApiConfig wxConfig){
        return doGet(WxApiUrlEnum.GET_TOKEN,ApiUtil.objectToMap(wxConfig));
    }

    /**
     * 读取成员列表
     * @param param
     * @return
     */
    public static WxUserListResponse getUserList(GetUserListReq param){
        String result = doGet(WxApiUrlEnum.GET_USERLIST,ApiUtil.objectToMap(param));
        WxUserListResult wxUserListResult = JSON.parseObject(result, WxUserListResult.class);
        if (SdkReturnEnum.SUCCESS.getCode().equals(wxUserListResult.getErrcode())) {
            WxUserListResponse response = new WxUserListResponse();
            response.setReturnCode(wxUserListResult.getErrcode());
            response.setReturnMsg(wxUserListResult.getErrmsg());
            List<WxUserModel> userList = new ArrayList<>();
            if (!CollUtil.isEmpty(wxUserListResult.getUserlist())) {
                wxUserListResult.getUserlist().forEach(item->{
                    WxUserModel model = new WxUserModel();
                    model.setUserId(item.getUserid());
                    model.setName(item.getName());
                    model.setDepartment(item.getDepartment());
                    model.setOpenUserId(item.getOpen_userid());
                    userList.add(model);
                });
            }
            return WxUserListResponse.ok(response);
        } else {
            return WxUserListResponse.error(
                    wxUserListResult.getErrcode(),wxUserListResult.getErrmsg(), new WxUserListResponse());
        }
    }

    /**
     * 读取成员
     * @param param
     * @return
     */
    public static WxUserInfoResponse getUser(GetUserReq param){
        String result =  doGet(WxApiUrlEnum.GET_USER,ApiUtil.objectToMap(param));
        WxUserInfo userInfo = JSON.parseObject(result, WxUserInfo.class);
        if (SdkReturnEnum.SUCCESS.getCode().equals(userInfo.getErrcode())) {
            WxUserInfoResponse response = new WxUserInfoResponse();
            response.setUserInfo(userInfo);
            return WxUserInfoResponse.ok(response);
        } else {
            return WxUserInfoResponse.error(
                    userInfo.getErrcode(),userInfo.getErrmsg(), new WxUserInfoResponse());
        }

    }
}
